package com.ruoyi.web.controller.business;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BProjectTask;
import com.ruoyi.system.domain.vo.BProjectTaskVo;
import com.ruoyi.system.domain.vo.BUserVo;
import com.ruoyi.system.service.BProjectTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目任务管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/project/task")
public class ProjectTaskController extends BaseController {

    @Autowired
    BProjectTaskService projectTaskService;

    /**
     * 获取项目任务列表
     */
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:list')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping("/list")
    public TableDataInfo list(BProjectTask projectTask)
    {
        startPage();
        List<BProjectTaskVo> list = projectTaskService.selectTaskList(projectTask);
        return getDataTable(list);
    }

    /**
     * 获取用户列表
     */
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:userList')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping("/userList")
    public TableDataInfo userList(BProjectTaskVo projectTask)
    {
        List<BUserVo> list = projectTaskService.selectUserListByProjectId(projectTask.getProjectId(),projectTask.getType());
        return getDataTable(list);
    }
    /**
     * 根据项目任务Id获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:query')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable Long taskId) {
        BProjectTaskVo projectTaskVo = projectTaskService.selectTaskById(taskId);
        return success(projectTaskVo);
    }

    /**
     * 新增项目任务
     */
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:add')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "新增项目任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BProjectTaskVo projectTaskVo)
    {
        return toAjax(projectTaskService.saveVo(projectTaskVo));
    }

    /**
     * 修改项目任务
     */
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:edit')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "修改项目任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BProjectTaskVo projectTaskVo)
    {
        return toAjax(projectTaskService.updateVo(projectTaskVo));
    }

    /**
     * 删除项目任务
     */
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:remove')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @Log(title = "删除项目任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(projectTaskService.deleteTaskByIds(taskIds));
    }

    @Log(title = "导出任务数据", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('dict:projectTask:export')")
    @PreAuthorize("@ss.hasRole('qygl')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BProjectTask projectTask)
    {
        List<BProjectTaskVo> list = projectTaskService.exportTaskList(projectTask);
        logger.info("查询任务数据列表大小：" + list.size());
        logger.info("查询任务数据列表：" + JSONObject.toJSONString(list));

        ExcelUtil<BProjectTaskVo> util = new ExcelUtil<>(BProjectTaskVo.class);
        util.exportExcel(response, list, "任务数据");
    }
}
